import logging

# LOG_FILE = 'mylog.log'
# file_handler = logging.FileHandler(LOG_FILE) #输出到文件
console_handler = logging.StreamHandler()  #输出到控制台
# file_handler.setLevel('ERROR')     #error以上才输出到文件
console_handler.setLevel('INFO')   #info以上才输出到控制台
fmt = '%(asctime)s - %(funcName)s - %(lineno)s - %(levelname)s - %(message)s'  
formatter = logging.Formatter(fmt) 
# file_handler.setFormatter(formatter) #设置输出内容的格式
console_handler.setFormatter(formatter)
logger = logging.getLogger('updateSecurity')
logger.setLevel('DEBUG')     #设置了这个才会把debug以上的输出到控制台
# logger.addHandler(file_handler)    #添加handler
logger.addHandler(console_handler)
################################################ HUA LI DE FEN GE XIAN ##############################################################################
import json
import time
from Crypto.Hash import SHA1, SHA256, SHA512, MD5, HMAC


def get_msg_auth_code(api_route, secret_key, timestamp, json_str, method):
    print(f'body is {json_str}')
    # 拼接secret
    secret = 'ts@%s|route@%s|sec@%s|method@%s' %(timestamp, api_route, secret_key, method)
    print(f'secret is {secret}')
    # 转为字节
    sec_bytes = bytes(secret, encoding='utf-8')
    # 生成一个64字节的key，与sha256的分组长度相同
    mac_key = SHA512.new(sec_bytes).digest()
    print(f'key is {mac_key.hex()}')
    # 入参转为字节
    body_bytes = bytes(json_str, encoding='utf-8')
    # 执行HMAC
    msg_auth_code = HMAC.new(mac_key, body_bytes, SHA256).hexdigest()
    print(f'sign is {sign}')
    return msg_auth_code

jdict={'1': 222,'2':999,'3': ['22', 1]}

jdict=[
        {
          "number": "GZFF202308310001",
          "x_char_yc7cxccdhz": "资产",
          "x_many2one_u79y6cfk2c": "1",
          "x_selection_flixysbvpe": "key1",
          "x_many2one_mcjugffsyb": 5,
          "x_date_sbwpawerkq": "2023-08-31",
          "wbdjbt": [
            {
              "x_char_s9z7xly0wm": "收ZYY-00001入固定资产",
              "x_many2one_kkzzpnyqej": "1959",
              "x_many2one_5mlh5yseap": "康复中心门诊后楼",
              "x_many2one_fakn9b664v": 1856,
              "x_selection_9zilgtvjir": "自由",
              "x_float_ynag4twq6b": "100.00000",
              "x selection vihwrkst83": "key1",
              "x_many2one_jnvdhnirgu": "28"
            }
          ]
        }
      ]
s = 'KBfP5FQuZKdrUp5e3zaz6nqe7dWsFwxc'
jstr = json.dumps(jdict, separators=(',', ':'), ensure_ascii=False)  # 去掉空格
ts = 1701325760 or int(time.time())  # 10位数字
route = '/studio/api_special/insuite/zw/wbdj'
sign = get_msg_auth_code(route, s, ts, jstr, 'post')
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1684465219)))

